﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using HA.ModFlow.Application;
using HAGISV2;
using HAUtil;

using System.Windows.Threading;

using System.Reflection;
namespace HA.ModFlow.Presentation
{
    /// <summary>
    /// MainWindowV2.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindowV2 : Microsoft.Windows.Controls.Ribbon.RibbonWindow, IProgressHandler
    {
        public MainWindowV2()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainWindowV2_Loaded);
        }

        void MainWindowV2_Loaded(object sender, RoutedEventArgs e)
        {
            bool b = HA.ModFlow.Application.Services.AuthenticationService.DefaultService.AuthenticationNetWork();
            if (b)
            {

            }
            else
            {
                //Thread.CurrentThread.Abort(); 

                this.Close();

            }
        }

        private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
        {
          



           
        }

        private void DockManager_Loaded(object sender, RoutedEventArgs e)
        {

            //第一步进行UI配置

            Render3dModule.DefaultModule.VtkControl = this.rendererView.VtkMap;

            GisModule.DefaultModule.DefaultMap = this.rendererView.GisMap;
        



       

            //现在通过统一的管理器来执行绑定过程

          

            ModFlow.Application.CommandManager.BindCommandsToWindow(this);

            ModFlow.Application.CommandManager.BindKeyToWindow(this);





            //视图信息初始化


            UIModule.DefaultModule.RibbonView = this.ribbonView;
            UIModule.DefaultModule.RendererView = this.rendererView;

            UIModule.DefaultModule.ProjectView = this.projectView;


            UIModule.DefaultModule.DockingManager = this.DockManager;

            UIModule.DefaultModule.DocumentContent = this.mainView;



            //初始化布局变量

            

            StringBuilder sb=new StringBuilder ();
            System.IO.StringWriter sw = new System.IO.StringWriter(sb);

            
            this.DockManager.SaveLayout(sw);

            ConfigService.DefaultService.SettingInfo.DefaultLayoutString = sb.ToString ();





            //在这边先添加一个临时的坐标显示系统

            this.rendererView.GisMap.MouseMove += new System.Windows.Forms.MouseEventHandler(GisMap_MouseMove);




            ///查找是否有启动参数
            ///
            string[] args  = System.Environment.GetCommandLineArgs();

            string fileName="";



            #region 原有的


            //if (args != null)
            //{
            //    if (args.Length > 1)
            //    {
            //        //fileName = args[0];//exe的路径
            //        //fileName = args[1];//文件路径

            //        if (args.Length > 2)
            //        {
            //            for (int index = 1; index < args.Length; index++)
            //            {

            //                fileName = fileName + " " + args[index];

            //            }
            //        }

            //        if (System.IO.Path.GetExtension(fileName) == ".hmf")
            //        {
            //            ProjectSerivce.DefaultService.Open(fileName);

            //            //程序配置操作

            //            if (!ProjectSerivce.DefaultService.BNullProj)
            //            {
            //                History h = new History(DateTime.Now, ProjectSerivce.DefaultService.GetProjMainFile(), ProjectSerivce.DefaultService.GetProjName());

            //                ConfigService.DefaultService.SettingInfo.AddHistory(h);
            //            }
            //        }

            //    }
            //}

            #endregion


            #region 尝试

            if (args != null)
            {
                if (args.Length > 1)
                {
                    //.................fileName = args[0];//exe的路径
                    //............./fileName = args[1];//文件路径

                    fileName = args[1];

                    //  ...............      If args.Length > 2 Then

                    // .............           For i As Integer = 2 To args.Length - 1

                    //  ...............              filename = filename + " " + args(i)

                    // ............           Next
                    // ...........       End If




                    if (args.Length > 2)
                    {
                        for (int index = 2; index < args.Length; index++)
                        {

                            fileName = fileName + " " + args[index];

                        }
                    }


                    // .................       If System.IO.Path.GetExtension(filename) = ".nos" Then

                    //  .............          Me.Open(filename)

                    // .............           Me.frmGis.Map1.FunctionMode = DotSpatial.Controls.FunctionMode.Pan
                    //  .............          Me.ToolStripStatusLabel.Text = "就绪"
                    // ............           Return

                    //  ...............      End If

                    if (System.IO.Path.GetExtension(fileName) == ".hmf")
                    {
                        ProjectService.DefaultService.Open(fileName);

                        //程序配置操作

                        if (!ProjectService.DefaultService.BNullProj)
                        {
                            History h = new History(DateTime.Now, ProjectService.DefaultService.GetProjMainFile(), ProjectService.DefaultService.GetProjName());

                            ConfigService.DefaultService.SettingInfo.AddHistory(h);
                        }
                    }

                }
            }




            #endregion



            #region 噪声的

            //'这里开始去的程序开始时的参数

        //Dim args As String() = System.Environment.GetCommandLineArgs()

        //If args IsNot Nothing Then

        //    If args.Length > 1 Then

        //        Dim filename = args(1)

        //        If args.Length > 2 Then

        //            For i As Integer = 2 To args.Length - 1

        //                filename = filename + " " + args(i)

        //            Next
        //        End If

        //        If System.IO.Path.GetExtension(filename) = ".nos" Then

        //            Me.Open(filename)

        //            Me.frmGis.Map1.FunctionMode = DotSpatial.Controls.FunctionMode.Pan
        //            Me.ToolStripStatusLabel.Text = "就绪"
        //            Return

        //        End If

        //    End If

        //End If


            #endregion


            ////伪代码  显示信息
            //string conctent = "";
            //for (int i = 0; i < args.Length ; i++)
            //{
            //    conctent = "5"+conctent + " 5 " + args[i].ToString() + "5 ";
            //}

            ////伪代码  显示信息
            ////System.Windows.MessageBox.Show(args.Length.ToString() + conctent);
            ////System.Windows.MessageBox.Show(args.Length.ToString()+"   5  " + args [3].ToString ());
            //System.Windows.MessageBox.Show(fileName.ToString());
            ////System.Windows.MessageBox.Show(fileName.ToString() + "   /r/n " + args.Length.ToString() + "   " + conctent);


            ModFlow.Application.CommandManager.InitMainWindow(this);
        }

        void GisMap_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            DotSpatial.Topology.Coordinate coord = null;
            coord = this.rendererView.GisMap.PixelToProj(e.Location);
            tbCoords.Text = coord.X.ToString("F04") + "," + coord.Y.ToString("F04");

            try
            {
                if (GisModule.DefaultModule.GisManager.InteractiveSystem.IsUsingGridEditMode)
                {
                    HydrolicGridLayer hyGridLayer = (HydrolicGridLayer)GisModule.DefaultModule.GisManager.LayerSystem.LayerCollection.GetLayer(GisModule.DefaultModule.GridLayerId);

                    DotSpatial.Data.IFeature f = hyGridLayer.PyLayer.DataSet.Features.FirstOrDefault(h => (h.BasicGeometry as DotSpatial.Topology.Polygon).Intersects(coord.X, coord.Y));

                    if (f == null)
                    {
                        tbKeyMessage.Text = "";
                        return;
                    }

                    int IDNumber = hyGridLayer.PyLayer.DataSet.Features.IndexOf(f)+1;
                    hyGridLayer.ComputeSelectedCellIndexs(ref IDNumber);

                    int[] ijk = ProjectService.DefaultService.Project.MfData.GridData.GetIJK(IDNumber);

                    tbKeyMessage.Text = "ID:" + string.Format("{0,-7}", IDNumber) + " 行:" + string.Format("{0,-5}",ijk[0]) + " 列:" + string.Format("{0,-5}",ijk[1]) + " 层:" + string.Format("{0,-5}",ijk[2]);
                }
                else
                    tbKeyMessage.Text = "";
            }
            catch
            {
                tbKeyMessage.Text = "";
            }
        }



        protected delegate void UpdateProg(string key, int percent, string message);


        /// <summary>
        /// 要写一个线程安全的方法，我的头好晕啊
        /// </summary>
        /// <param name="Message"></param>
        /// <param name="Error"></param>
        public void Progress(string key, int percent, string message)
        {


            UpdateProg prg = UpdateProgress;

            this.Dispatcher.BeginInvoke(prg, DispatcherPriority.Normal, new object[] {key,percent,message});





        }

        public void UpdateProgress(string key, int percent, string message)
        {

            //实际执行的写信息函数
            //更新对应的流程关系


            this.tbKeyMessage.Text = key.ToString()+" "+message.ToString ();
            this.pbProcess.Value = percent;





        }
    }
}
